/*
 *   Copyright 2006 Wei Lian <lianwei3@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "header.h"
pid_t pid_player;
extern GtkWidget *entry_player;
extern GtkLabel *label_player;

void
fork_player (char *url)
{
  GdkColor color;
  gdk_color_parse ("blue", &color);
  gtk_widget_modify_fg (GTK_WIDGET (label_player), GTK_STATE_ACTIVE, &color);

  pid_t pid;
  pid = fork ();
  if (pid == 0)
    {
      char *line, *playopt[32], *savept = NULL;
      int iter = 0;

      line = strdup (gtk_entry_get_text (GTK_ENTRY (entry_player)));

      playopt[0] = strtok_r (line, " ,", &savept);
      while ((playopt[++iter] = strtok_r (NULL, " ,", &savept)) != NULL);

      playopt[iter] = url;
      playopt[++iter] = NULL;

      int fd = open ("/dev/zero", O_WRONLY);
      dup2 (fd, 1);
      dup2 (fd, 2);
      execvp (playopt[0], playopt);
      _exit (-1);
    }
  else if (pid > 0)
    pid_player = pid;
}

void
kill_process (pid_t pid)
{
  if (pid > 0)
    kill (pid, SIGTERM);
}
